using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication
{
	/// <summary>
	/// Summary description for BuyTrain.
	/// </summary>
	public partial class BuyTrain : System.Web.UI.Page
	{
	
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void Page_Load(object sender, System.EventArgs e)
		{
            int             nTrain  = -1;
            bool            bBank   = false;
            string          sTrain;
            Game	        game	= Global.getGame (this);;
            PublicCompany   company;

            company = game.CurrentlyOperatingCompany;

            //check if forced train sale
            if (company.ForcedToBuyTrain())
            {
                // find out where cheapest train is and enable accordingly
				int cheapestOpenMarketTrain, cheapestBankTrain;
                company.FindCheapestTrain(out bBank, out nTrain, out sTrain, out cheapestOpenMarketTrain, out cheapestBankTrain);

                NewTrain.Enabled        = (cheapestBankTrain <= company.Cash) || bBank;
                OpenMarketTrain.Enabled = (cheapestOpenMarketTrain <= company.Cash) || !bBank;
                ExistingTrain.Enabled   = company.Cash > 0;
                TradeInTrain.Enabled    = false;
            }
            else
            {
                OpenMarketTrain.Enabled = game.Trains_OpenMarket.Count > 0;	                // are any available?
                ExistingTrain.Enabled   = company.Cash > 0;
                TradeInTrain.Enabled    = company.Cash > 0 && game.Trains_6.Count <= 1;	    // was a 6 train purchased?
            }
		}

		protected void NewTrain_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("BuyNewTrain.aspx");
		}

		protected void ExistingTrain_Click(object sender, System.EventArgs e)
		{
			Game	game	= Global.getGame (this);;

			game.ResetExistingTrainSaleInfo();
			game.ExistingTrainPriorityPlayerName = game.CurrentOperatingOwner();
			game.ExistingTrainSalePending = true;

			Response.Redirect("BuyExistingTrain.aspx");
		}

		protected void OpenMarketTrain_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("BuyOpenMarketTrain.aspx");
		}

		protected void TradeInTrain_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("TradeInTrain.aspx");
		}

		protected void OperatingRound_Click(object sender, System.EventArgs e)
		{
            Game game = Global.getGame(this); ;
            game.UpdateAllPlayers ();
            Response.Redirect("OperatingRound2.aspx");
		}

	}
}
